package sseGameApps.service.mdkbr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slim3.datastore.Datastore;

import com.google.appengine.api.datastore.Transaction;

import sseGameApps.meta.mdkbr.MdKanjiMeta;
import sseGameApps.meta.mdkbr.MdUseKanjiMeta;
import sseGameApps.meta.mdkbr.MdWorldMapMeta;
import sseGameApps.model.mdkbr.MdKanji;
import sseGameApps.model.mdkbr.MdUseKanji;
import sseGameApps.model.mdkbr.MdWorldMap;

public class MdStrategyService {

    private MdWorldMapMeta w = new MdWorldMapMeta();
    private MdKanjiMeta l = new MdKanjiMeta();
    private MdUseKanjiMeta e = new MdUseKanjiMeta();

    public MdWorldMap getMdWorldMap(String mapId) {

        return Datastore.query(w).filter(w.mapId.equal(mapId)).asSingle();

    }

    public List<MdWorldMap> getMdWorldMapList() {

        return Datastore.query(w).sort(w.mapId.asc).asList();

    }

    public List<HashMap<String, String>> getAreaData(String userId, String mapId) {

        List<HashMap<String, String>> list =
            new ArrayList<HashMap<String, String>>();

        List<String> areaTypeList = getAreaTypeData(mapId);

        Integer userCount = getMdUseKanjiCount();

        MdUseKanji mdUseKanji = new MdUseKanji();

        for (String areaType : areaTypeList) {
            HashMap<String, String> areaData = new HashMap<String, String>();
            areaData.put("areaType", areaType);
            if ("0".equals(areaType) || "1".equals(areaType)) {
                areaData.put("areaUserId", "");
                areaData.put("areaBP", "????");
                areaData.put("areaAP", "????");
                areaData.put("areaKanji1", "");
                areaData.put("areaKanji2", "");
                areaData.put("areaKanji3", "");
                areaData.put("areaImage", "");
            } else if ("2".equals(areaType) || "3".equals(areaType)) {
                if ("2".equals(areaType)) {
                    mdUseKanji =
                        getRundomMdUseKanji(
                            userId,
                            (int) (Math.round(Math.random() * userCount + 0.5)));
                } else {
                    mdUseKanji = getMdUserKanji(userId);
                }
                if (mdUseKanji == null) {
                    areaData.put("areaUserId", "");
                    areaData.put("areaBP", "????");
                    areaData.put("areaAP", "????");
                    areaData.put("areaKanji1", "");
                    areaData.put("areaKanji2", "");
                    areaData.put("areaKanji3", "");
                    areaData.put("areaImage", "");
                } else {
                    areaData.put("areaUserId", mdUseKanji.getUserId());
                    Map<String, String> kanjiStatus =
                        getKanjiStatus(
                            mdUseKanji.getHolderKanji01(),
                            mdUseKanji.getHolderKanji02(),
                            mdUseKanji.getHolderKanji03());
                    areaData.put("areaBP", kanjiStatus.get("barPoint"));
                    areaData.put("areaAP", kanjiStatus.get("attackPoint"));
                    areaData.put("areaKanji1", mdUseKanji.getHolderKanji01());
                    areaData.put("areaKanji2", mdUseKanji.getHolderKanji02());
                    areaData.put("areaKanji3", mdUseKanji.getHolderKanji03());
                    areaData.put("areaImage", "");
                }
            } else {
                areaData.put("areaUserId", "");
                areaData.put("areaBP", "????");
                areaData.put("areaAP", "????");
                areaData.put("areaKanji1", "");
                areaData.put("areaKanji2", "");
                areaData.put("areaKanji3", "");
                areaData.put("areaImage", "");
            }

            list.add(areaData);
        }

        return list;
    }

    public List<String> getAreaTypeData(String mapId) {

        List<String> list = new ArrayList<String>();

        MdWorldMap mdWorldMap = getMdWorldMap(mapId);

        if (mdWorldMap == null) {
            return null;
        }

        list.add(mdWorldMap.getAreaType01());
        list.add(mdWorldMap.getAreaType02());
        list.add(mdWorldMap.getAreaType03());
        list.add(mdWorldMap.getAreaType04());
        list.add(mdWorldMap.getAreaType05());
        list.add(mdWorldMap.getAreaType06());
        list.add(mdWorldMap.getAreaType07());
        list.add(mdWorldMap.getAreaType08());
        list.add(mdWorldMap.getAreaType09());
        list.add(mdWorldMap.getAreaType10());
        list.add(mdWorldMap.getAreaType11());
        list.add(mdWorldMap.getAreaType12());
        list.add(mdWorldMap.getAreaType13());
        list.add(mdWorldMap.getAreaType14());
        list.add(mdWorldMap.getAreaType15());
        list.add(mdWorldMap.getAreaType16());
        list.add(mdWorldMap.getAreaType17());
        list.add(mdWorldMap.getAreaType18());
        list.add(mdWorldMap.getAreaType19());
        list.add(mdWorldMap.getAreaType20());
        list.add(mdWorldMap.getAreaType21());
        list.add(mdWorldMap.getAreaType22());
        list.add(mdWorldMap.getAreaType23());
        list.add(mdWorldMap.getAreaType24());
        list.add(mdWorldMap.getAreaType25());
        list.add(mdWorldMap.getAreaType26());
        list.add(mdWorldMap.getAreaType27());
        list.add(mdWorldMap.getAreaType28());
        list.add(mdWorldMap.getAreaType29());
        list.add(mdWorldMap.getAreaType30());

        return list;

    }

    public MdUseKanji getRundomMdUseKanji(String userId, Integer rundomNum) {

        return Datastore
            .query(e)
            .filter(e.userId.notEqual(userId))
            .offset(rundomNum - 1)
            .limit(1)
            .asSingle();
    }

    public MdUseKanji getMdUserKanji(String userId) {

        return Datastore.query(e).filter(e.userId.equal(userId)).asSingle();
    }

    public Integer getMdUseKanjiCount() {

        return Datastore.query(e).count();

    }

    public Map<String, String> getKanjiStatus(String kanji1, String kanji2,
            String kanji3) {

        Integer barPoint = 0;
        Integer attackPoint = 0;
        Map<String, String> kanjiStatusData = new HashMap<String, String>();

        List<MdKanji> list =
            Datastore
                .query(l)
                .filter(l.mojiText.in(kanji1, kanji2, kanji3))
                .asList();

        if (list != null) {
            for (MdKanji mdKanji : list) {
                barPoint = barPoint + mdKanji.getBarPoint();
                attackPoint = attackPoint + mdKanji.getAttackPoint();
            }
        } else {
            barPoint = (int) (Math.round(Math.random() * 9999 + 0.5));
            attackPoint = (int) (Math.round(Math.random() * 200 + 0.5)) + 200;
        }

        kanjiStatusData.put("barPoint", barPoint.toString());
        kanjiStatusData.put("attackPoint", attackPoint.toString());

        return kanjiStatusData;

    }

    public void registMdWorldMap() {

        MdWorldMap data = new MdWorldMap();

        data.setMapId("MAP01");
        data.setAreaNum(5);
        data.setAreaType01("0");
        data.setAreaType02("0");
        data.setAreaType03("0");
        data.setAreaType04("0");
        data.setAreaType05("0");
        data.setAreaType06("0");
        data.setAreaType07("2");
        data.setAreaType08("0");
        data.setAreaType09("2");
        data.setAreaType10("1");
        data.setAreaType11("0");
        data.setAreaType12("1");
        data.setAreaType13("0");
        data.setAreaType14("0");
        data.setAreaType15("3");
        data.setAreaType16("0");
        data.setAreaType17("0");
        data.setAreaType18("0");
        data.setAreaType19("0");
        data.setAreaType20("0");
        data.setAreaType21("0");
        data.setAreaType22("0");
        data.setAreaType23("0");
        data.setAreaType24("0");
        data.setAreaType25("0");
        data.setAreaType26("0");
        data.setAreaType27("0");
        data.setAreaType28("0");
        data.setAreaType29("0");
        data.setAreaType30("0");

        Transaction tx = Datastore.beginTransaction();
        Datastore.put(data);
        tx.commit();

    }
}
